<?php

class SearchPageController extends Jwawa_BaseController
{
    public function delPageByIdsAction(){
        $param = $this->getJsonDataFromReq();
        $ids = array();
        foreach($param['ids'] as $id){
            array_push($ids, intval($id));
        }
        Model_Cfg_SearchPage::delPageByIds($ids);


        $rst = new Jwawa_ResponseBaseDto();
        $json = Zend_Json::encode($rst);
        echo $json;
    }

    public function getPageByIdAction(){
        $param = $this->getJsonDataFromReq();
        $searchPage = Model_Cfg_SearchPage::getPageById($param['id']);

        $rst = new Jwawa_ResponseBaseDto($searchPage);
        $json = Zend_Json::encode($rst);
        echo $json;
    }
    
    public function getPageAndItemByIdAction(){
        $param = $this->getJsonDataFromReq();
        $searchPage = Model_Cfg_SearchPage::getPageById($param['id']);
        $searchItemList = Model_Cfg_SearchItem::findSearchItemByPageCode($searchPage->page_code);

        $rst = new Jwawa_ResponseBaseDto(array(
            'searchPage' => $searchPage,
            'searchItemList' => $searchItemList
        ));
        $json = Zend_Json::encode($rst);
        echo $json;
    }

    public function findSearchPageListAction(){
        $param = $this->getJsonDataFromReq();

        $sbr = new Jwawa_Db_SearchBuilder($param['searchPageCode'],
                                          $param['searchItems'],
                                          $param['start'],
                                          $param['limit']);
        $total = Model_Cfg_SearchPage::countPageListBySbr($sbr);
        $pageList = Model_Cfg_SearchPage::findPageListBySbr($sbr);

        $rst = new Jwawa_ResponseBaseDto($pageList, true, $total);
        $json = Zend_Json::encode($rst);
        echo $json;
    }

    public function saveSearchPageAction(){
        $param = $this->getJsonDataFromReq();
        $now = date("Y-m-d H:i:s");
        $tmp = array('id' => empty($param['id']) ? null : $param['id'],
                     'entity_header_id' => $param['entity_header_id'],
                     'action_id' => $param['action_id'],
                     'page_name' => $param['page_name'],
                     'page_code' => $param['page_code'],
                     'is_deleted' => 'N',
                     'update_by' => $this->getSessionBag()->getLoginName(),
                     'update_time' => $now
        );

        if(empty($param['id'])){
             $tmp['create_by'] = $this->getSessionBag()->getLoginName();
             $tmp['create_time'] = $now;
        }

        if(!empty($param['id'])){
            $oldSearchPage = Model_Cfg_SearchPage::getPageById($param['id']);
        }
        $searchPage = new Model_Cfg_SearchPage($tmp);

        $db = Model_Cfg_SearchPage::getDb();
        try{
            $db->beginTransaction();
            $searchPage = Model_Cfg_SearchPage::saveOrUpdateSearchPage($searchPage);
            if(isset($oldSearchPage) && $oldSearchPage->entity_header_id != $searchPage->entity_header_id){
                Model_Cfg_SearchItem::delSearchItemByPageId($searchPage->id);
            }
            $db->commit();
        } catch (Exception $e){
            $db->rollBack();
            throw $e;
        }

        $rst = new Jwawa_ResponseBaseDto($searchPage);
        $json = Zend_Json::encode($rst);
        echo $json;
    }

    // -------------SearchItem CRUD---------------------

    public function changeSearchItemSeqAction(){
        $param = $this->getJsonDataFromReq();
        $db = Model_Cfg_SearchGroup::getDb();
        try{
            $db->beginTransaction();
            switch($param['optFlag']){
                case 'top':
                    Model_Cfg_SearchItem::changeSeqToTop($param['searchItemId']);
                    break;
                case 'up':
                    Model_Cfg_SearchItem::changeSeqToUp($param['searchItemId']);
                    break;
                case 'down':
                    Model_Cfg_SearchItem::changeSeqToDown($param['searchItemId']);
                    break;
                case 'bottom':
                    Model_Cfg_SearchItem::changeSeqToBottom($param['searchItemId']);
                    break;
            }
            $db->commit();
        } catch (Exception $e){
            $db->rollBack();
            throw $e;
        }

        $rst = new Jwawa_ResponseBaseDto();
        $json = Zend_Json::encode($rst);
        echo $json;
    }

    public function saveSearchItemAction(){
        $param = $this->getJsonDataFromReq();
        $now = date("Y-m-d H:i:s");
        $tmp = array('id' => empty($param['id']) ? null : intval($param['id']),
                     'page_id' => $param['page_id'],
                     'entity_detail_id' => $param['entity_detail_id'],
                     'item_type' => $param['item_type'],
                     'item_class' => $param['item_class'],
                     'item_cfg' => $param['item_cfg'],
                     'condition_flag' => $param['condition_flag'],
                     'is_deleted' => 'N',
                     'create_by' => $this->getSessionBag()->getLoginName(),
                     'update_by' => $this->getSessionBag()->getLoginName(),
                     'create_time' => $now,
                     'update_time' => $now
        );
        $searchItem = new Model_Cfg_SearchItem($tmp);
        $searchItem = Model_Cfg_SearchItem::saveOrUpdate($searchItem);
        $searchItem = Model_Cfg_SearchItem::getSearchItemById($searchItem->id);
        $rst = new Jwawa_ResponseBaseDto($searchItem);
        $json = Zend_Json::encode($rst);
        echo $json;
    }

    public function getSearchItemByIdAction(){
        $param = $this->getJsonDataFromReq();
        $searchItem = Model_Cfg_SearchItem::getSearchItemById($param['id']);

        $rst = new Jwawa_ResponseBaseDto($searchItem);
        $json = Zend_Json::encode($rst);
        echo $json;
    }

    public function delSearchItemByIdAction(){
        $param = $this->getJsonDataFromReq();

        $db = Model_Cfg_SearchItem::getDb();
        try{
            $db->beginTransaction();
            Model_Cfg_SearchItem::delSearchItemById($param['id']);
            Model_Cfg_SearchGroupItemShip::deletBySearchItemId($param['id']);
            $db->commit();
        } catch (Exception $e){
            $db->rollBack();
            throw $e;
        }

        $rst = new Jwawa_ResponseBaseDto();
        $json = Zend_Json::encode($rst);
        echo $json;
    }
}
